home *** CD-ROM | disk | FTP | other *** search
- /* bt_del.c - delete function */
- #include "stdio.h"
- #include "btree.h"
- #include "bt_macro.h"
- extern IX_DESC *pci ;
- extern int split_size ;
- extern int comb_size ;
- BLOCK *neighbor() ;
-
- int delete_ix(pix) /* delete current entry */
- IX_DESC *pix ; /* points to index descriptor */
- { /* returns success=1 , failure=0 */
- ENTRY tempe ;
- BLOCK b ;
- int ret ;
-
- pci = pix ;
- /* check for dummy entry at end-of-ix */
- copy_curreny(0,&tempe) ;
- if( call(pci->pcomp)(&tempe,& pci->dx.dume) == 0 )
- return( IX_FAIL ) ;
- /* not at end - delete it */
- ret = del_level(0,&b) ;
-
- return( ret ) ;
- }
-
-
- int del_level(l,pb) /* delete entry within the level */
- int l ;
- BLOCK *pb ;
- {
- RECPOS r ;
- int ret ;
- ENTRY tempe ;
-
- ret = IX_OK ;
- retrieve_block(l,CB(l),pb,CURR) ; /* get current block */
- del_block(pb,CO(l)) ; /* delete the entry in the block */
-
- if( pb->bend == 0 ) /* block now empty ? */
- { put_free(pb) ; /* yes - free the block */
- ret = del_level(l+1,pb) ; /* delete entry for empty block */
- copy_current(l+1,&tempe) ; /* get new current block pointer */
- first_ix(l,pb,tempe.rptr) ; /* reset pos for lower levels */
- return( ret ) ;
- }
- if( CO(l) >= pb->bend ) /* last entry in block deleted ? */
- { /* yes - correct upper index */
- fix_last(l,pb->brec,ENT_ADR(pb,last_entry(pb))) ;
- }
- if( pb->bend < comb_size) /* less than half full ? */
- ret + compress(l,pb) ; /* yes - try combine with neighbor */
- else update_block(pb) ;
- retrieve_block(l,CB(l),pb,CURR) ; /* get our block again */
- if(CO(l) >= pb->bend ) /* is position past end of block ? */
- first_ix(l,pb,next_ix(l+1)) ; /* yes - move to next block */
- return( ret ) ;
- }
-
-
- int compress(l,pb) /* combine a block with a neighbor */
- int l ;
- BLOCK *pb ; /* the block to be combined */
- {
- int nb ;
- BLOCK *pt ;
- ENTRY tempe ;
-
-
- if( (l+1) == pci->dx.nl ) /* is this the root level ? */
- { update_block(pb) ; /* yes - update the block */
- return( IX_OK ) ; /* and return */
- }
-
- pt = neighbor(l,LEFTN) ; /* get left neighbor block */
- if( (pt != NULL )
- && (pt->bend + pb->bend <= IXB_SPACE) )
- { combine(pt,pb) ; /* combine blocks */
- update_block(pb) ; /* update right block */
- put_free(pt) ; /* free the left index block */
- /* CB(l) is ok as is */
- CO(l) = CO(l) + pt->bend ; /* adjust our current pos. */
- last_ix(l+1,pb) ; /* point higher level to left blk */
- del_level(l+1,pb) ; /* delete ptr. tp left block */
- return( IX_OK ) ;
- }
-
-
- pt = neighbor(l,RIGHTN) ; /* get right neighbor block */
- if( (pt != NULL )
- && (pt->bend + pb->bend <= IXB_SPACE) )
- { combine(pb,pt) ; /* combine blocks */
- update_block(pt) ; /* update right block */
- CB(l) = pt->brec ; /* right block is curr. one now */
- /* CO(l) is ok as is */
- put_free(pb) ; /* free the left index block */
- del_level(l+1,pb) ; /* delete ptr. tp left block */
- return( IX_OK ) ;
- }
-
- update_block(pb) ; /* can't combine - just update blk. */
- return( IX_OK ) ;
- }
-
- int fix_last(l,r,pe) /* fix higher level index */
- int l ; /* level we are on */
- RECPOS r ; /* rptr for higher level entry */
- ENTRY *pe ; /* entry with new key */
- {
- ENTRY tempe ;
- /* last entry in a block deleted/replaced */
- /* update higher level index */
-
- copy_entry(&tempe,pe) ; /* copy key */
- tempe.rptr = r ; /* put in the record pointer */
- return( replaced_entry(l+1,&tempe) ) ; /* replace the entry */
- }
-
-
- int replace_entry(l,pe) /* replace current index entry */
- int l ; /* at this index level */
- ENTRY *pe ; /* new entry */
- {
- BLOCK b ;
- int ret ;
-
- retrieve_block(l,CB(l),&b,CURR) ; /* get the index block */
- if( CO(l) == last_entry(&b)) /* is this the last entry ? */
- fix_last(l,CB(l),pe) ; /* yes - fix up higher level */
- del_block(&b,CO(l)) ; /* remove the current entry */
- /* room to insert the new entry */
- if( (b.bend + ENT_SIZE(pe)) <= split_size )
- { ins_block(&b,pe,CO(l)) ; /* yes - insert in the block */
- update_block(&b) ; /* and update the block */
- ret = IX_OK ;
- }
- else ret = split(l,pe,&b) ; /* no - split the block */
- return( ret ) ;
- }